package com.wz.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExcelFileReader {
	private static Logger log = LoggerFactory.getLogger(ExcelFileReader.class);
	
	public static List<Map<Integer, Object>> readFromFile(String filePath) throws FileNotFoundException, IOException{
		List<Map<Integer, Object>> list = new ArrayList<Map<Integer,Object>>();
		File file = new File(filePath);
		POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
		@SuppressWarnings("resource")
		HSSFWorkbook hssfWorkbook =  new HSSFWorkbook(poifsFileSystem);
		HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(1);		//第几个选项卡

		int rowstart = hssfSheet.getFirstRowNum();
		int rowEnd = hssfSheet.getLastRowNum();
		for(int i=rowstart;i<=rowEnd;i++)
		{
			HSSFRow row = hssfSheet.getRow(i);
			if(null == row) {
				log.error("第" + i + "行是空行!");
				continue;
			}
			int cellStart = row.getFirstCellNum();
			int cellEnd = row.getLastCellNum();

			Map<Integer, Object> map = new HashMap<Integer, Object>();
			for(int k=cellStart;k<=cellEnd;k++)
			{
				HSSFCell cell = row.getCell(k);
				if(null==cell) {
					continue;
				}
				switch (cell.getCellType())
				{
				case HSSFCell.CELL_TYPE_NUMERIC: // 数字
					//	                                System.out.print(cell.getNumericCellValue() + "   ");
					map.put(k, cell.getNumericCellValue());
					break;
				case HSSFCell.CELL_TYPE_STRING: // 字符串
					//	                    System.out.print(cell.getStringCellValue() + "   ");
					map.put(k, cell.getStringCellValue());
					break;
				case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
					//	                    System.out.println(cell.getBooleanCellValue() + "   ");
					map.put(k, cell.getBooleanCellValue());
					break;
				case HSSFCell.CELL_TYPE_FORMULA: // 公式
//					System.out.print(cell.getCellFormula() + "   ");
					map.put(k, cell.getCellFormula());
					break;
				case HSSFCell.CELL_TYPE_BLANK: // 空值
					//	                    System.out.println(" ");
					map.put(k, "");
					break;
				case HSSFCell.CELL_TYPE_ERROR: // 故障
					//	                    System.out.println(" ");
					map.put(k, "WRONG_DATA");
					break;
				default:
//					System.out.print("未知类型   ");
					map.put(k, "UNKNOWN_DATA");
					break;
				}
			}
			list.add(map);
		}
		log.info("共读取数据:" + list.size() + "行");
		return list;
	}

	public static void main(String[] args) {
		try {
			ExcelFileReader.readFromFile("C://Users//wangzhi//Desktop//导数据//珠光置业（未核实）//gudongziliaofujian.xls");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}